#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include "u_service.h"
#include "unreliable_layer.h"
#include "u_peer_manager.h"
#include "login_server.h"

class ConnectionListener : public IConnectionListener
{
public:
	virtual void onConnectionLost(UPeer::Ptr upeer)
	{
		cout << "(listener)lost from :" + upeer->getRemoteAddr().address().to_string() << endl;
	}
	virtual void onNewConnectionConnected(UPeer::Ptr upeer) 
	{
		cout << "(listener)connected from :" + upeer->getRemoteAddr().address().to_string() << endl;
		
	}
};

class AuthHandler : public IAuthRequester
{
public:
	virtual void request(const string& userName, boost::shared_ptr<IAuthCallback> callback)
	{
		callback->onSetAuthInfo(userName, "everyone");
	}
};

int main(int args, char* argv[])
{
	boost::asio::io_service service;

	auto udpService = UdpService::newInstance(8080, service);
	boost::shared_ptr<UPeerManager> peerManager = UPeerManager::getInstace();
	auto unreliableLayer = boost::shared_ptr<UnreliableLayer>(
		new UnreliableLayer(peerManager, service));
	unreliableLayer->config(0.1, 50, 50);
	udpService->setPackageHandler(unreliableLayer);

	auto loginAttachment = new LoginServer();
	loginAttachment->setAuthRequester(boost::shared_ptr<IAuthRequester>(new AuthHandler()));

	peerManager->setConnectionListener(ConnectionListener::Ptr(loginAttachment));



	udpService->start();
	service.run();

	return 0;
}
